package com.micro.frog.fortune.model.base;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.micro.frog.fortune.util.PageUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * @author jason.lv
 * @since 2022/1/6 16:38
 */
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "分页列表响应")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageResp<T> extends ListResp<T> {

    @ApiModelProperty(value = "当前页码", example = "1")
    @JsonProperty("page")
    private long page = 1;

    @ApiModelProperty(value = "当前页码", example = "1")
    @JsonProperty("page_size")
    private long size = 10;

    @ApiModelProperty(value = "总记录数", example = "1")
    @JsonProperty("total")
    private long total = 0;

    @ApiModelProperty(value = "总页数", example = "1")
    @JsonProperty("total_pages")
    private long pages = 0;

    public static <T> PageResp<T> generate(List<T> items, long page, long size, long total) {
        PageResp<T> response = new PageResp<>();

        size = PageUtil.pageSize(size);

        response.setPage(page);
        response.setSize(size);
        response.setTotal(total);
        response.setPages(PageUtil.pages(total, size));
        response.setData(items);
        return response;
    }
}
